<template>
  <v-form ref="vForm" v-model="valid" lazy-validation>
    <v-row>
      <v-col cols="12" md="6">
        <v-text-field
          v-model="state.FirstName"
          :counter="10"
          :rules="nameRules"
          label="姓"
          required
        ></v-text-field
      ></v-col>
      <v-col cols="12" md="6">
        <v-text-field
          v-model="state.LastName"
          :counter="10"
          :rules="nameRules"
          label="名"
          required
        ></v-text-field>
      </v-col>
      <v-col cols="12" md="6">
        <v-text-field
          v-model="state.PhoneNumber"
          :counter="11"
          label="手机号"
          :rules="phoneRules"
          required
        ></v-text-field
      ></v-col>
      <v-col cols="12" md="6">
        <v-text-field
          v-model="state.Email"
          label="电子邮箱"
          :rules="emailRules"
        ></v-text-field>
      </v-col>
      <v-col cols="12">
        <v-text-field
          v-model="state.Address"
          label="地址"
          :rules="addrRules"
          clearable
        ></v-text-field>
      </v-col>
      <v-col cols="12">
        <BirthdayDataPiker v-model="state.DateOfBirth" />
      </v-col>
    </v-row>

    <v-btn color="primary" class="mr-4" @click="onSaveClick">
      保存
    </v-btn>

    <v-btn color="second" @click="onClearClick">
      重置
    </v-btn>
  </v-form>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from "vue-property-decorator";
import { PersonState } from "@/State/PersonState";
import { RequireValidator } from "@/Validators/RequireValidator";
import { Rules } from "@/Types/Types";
import { RegularExpressionValidator } from "@/Validators/RegularExpressionValidator";
import { FixedLengthValidator } from "@/Validators/FixedLengthValidator";
import { MinLengthValidator } from "@/Validators/MinLengthValidator";
import BirthdayDataPiker from "@/components/BirthdayDataPiker.vue";
import { VFormClass } from "@/shims-vuetify";

@Component({
  components: { BirthdayDataPiker },
  model: {
    event: "save"
  }
})
export default class PersonalForm extends Vue {
  // region Props

  // 在定义 Prop 属性时，不能直接赋初值，要使用default
  // default 需要定义为一个函数，并返回初始值
  @Prop({
    default: () => {
      return new PersonState();
    }
  })
  // @ts-ignore
  value: PersonState;
  // endregion

  // region Data
  valid = true;
  state:PersonState | null = null;
  // endregion

  $refs!: {
    vForm: VFormClass;
  };

  // region 检查规则
  nameRules: Rules = [RequireValidator];
  phoneRules: Rules = [
    RequireValidator,
    RegularExpressionValidator(
      "^^(188|133|136|170)",
      "手机号码号段只能为 188 133 136 170"
    ),
    FixedLengthValidator(11),
    RegularExpressionValidator("^[0-9]{11}$", "手机号必须全为数字")
  ];
  emailRules: Rules = [
    RegularExpressionValidator("^.+@.+$", "邮箱的格式应该为【用户名@域名】")
  ];
  addrRules: Rules = [MinLengthValidator(4)];
  // endregion

  @Watch("value",{immediate:true})
  onValue(){
    this.state = this.value;
    this.$refs.vForm.resetValidation()
  }

  // region 事件响应
  private onSaveClick(){
    if(this.$refs.vForm.validate()){
      this.$emit("save",this.state)
      this.$refs.vForm.resetValidation()
    }
  }

  private onClearClick() {
    this.$emit("clear");
    this.$refs.vForm.resetValidation()
  }

  // endregion
}
</script>
